using System;
using System.Diagnostics;
using UnityEngine;

namespace BitMango.Quest
{
	public abstract class Quest : MonoBehaviour
	{
		public abstract int GroupIndex { get; }

		public abstract int Index { get; }

		public abstract string Title { get; }

		public abstract string Description { get; }

		public abstract string RewardItemName { get; }

		public abstract int RewardCount { get; }

		public abstract string ID { get; }

		public string Key
		{
			get
			{
				return "BM_QUEST_" + this.ID;
			}
		}

		public STATE State
		{
			get
			{
				return (STATE)PlayerPrefs.GetInt(this.Key, 0);
			}
			set
			{
				if (value <= this.State)
				{
					return;
				}
				PlayerPrefs.SetInt(this.Key, (int)value);
				PlayerPrefs.Save();
				if (this.OnChangeState != null)
				{
					this.OnChangeState(this, value);
				}
			}
		}

		//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<Quest, STATE> OnChangeState;

		protected virtual bool IsAchieved
		{
			get
			{
				return false;
			}
		}

		private void OnEnable()
		{
			if (this.State == STATE.TRACKING && this.IsAchieved)
			{
				PlayerPrefs.SetInt(this.Key, 1);
				PlayerPrefs.Save();
			}
			QuestManager.Register(this);
		}

		public abstract void BeginTracking();

		public abstract void EndTracking();

		public const string SAVE_KEY_PREFIX = "BM_QUEST_";
	}
}
